# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exceptions import DropItem
import json
import codecs


class TjugsPipeline(object):
    def process_item(self, item, spider):
        return item

    def open_spider(self, spider):
        return spider

    def close_spider(self, spider):
        return spider


class ToJsonPipeline(object):
    lists = []
    file = codecs.open('result.json', 'wb+', 'utf-8')

    def process_item(self, item, spider):
        # line = json.dumps(dict(item))
        ToJsonPipeline.lists.append(dict(item))
        return item

    def close_spider(self, spider):
        # print ToJsonPipeline.lists
        ToJsonPipeline.file.write(json.dumps(ToJsonPipeline.lists, ensure_ascii=False, indent=2))
        print codecs.encode(json.dumps(ToJsonPipeline.lists, ensure_ascii=False), 'utf-8')

        return spider
